﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: LGPL, Software Libre	*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System.Collections.Generic;
using System.Net;
using Ciatel.ISO8583.Amex;
using Ciatel.ISO8583.Amex.Utilities;
using ESF.General;
using ESF.SSO.Core.Entities;
using ESF.SSO.Core.Entities.BasicReferences;
using ESF.General.Entity;
using System.Net.Sockets;

#endregion


namespace ESF.SSO.ISO8583.ClientRequestHandler.Operations
{
	internal static class SignIn_I
	{
		internal static void ProcessRequest (List<Message> pRequests, IPEndPoint pSenderIPEndPoint, IPEndPoint pForwarderIPEndPoint, out List<object> pProcessedObjects)
		{
			User user;
			
			Message message;
			
			CustomField customField_57;
			
			List<UserMenuItem> userMenuItems;
			
			int menuItemCount;
			
			string codMenusRoot;
			
			
			#region Primera Parte
			
			message = pRequests [0];
			
			message.ApplicationData.DataElements.CheckHasMandatoryFields
			(
				TheKnownFields.CustomPrivateField_57
			);
			
			user = new User ();
			
			customField_57 = message.ApplicationData.DataElements.GetCustomPrivateField_57 ();
			
			user.SessionID = customField_57.GetStringFieldWithVariableLength (40);					// Ans .. 40
			
			user.UserTerminal = 
				new Terminal () { 
					Location = new Location (){ Name = customField_57.GetStringFieldWithVariableLength (60), // Ans .. 60
						Address = customField_57.GetStringFieldWithVariableLength (60)	// Ans .. 60 
					} };
			
			user.Enterprise =
				new Enterprise () { IdentificationNumber = customField_57.GetStringFieldWithVariableLength (60), // Ans .. 60
					Name = customField_57.GetStringFieldWithVariableLength (60) };	// Ans .. 60	
			
			user.FirstName = customField_57.GetStringFieldWithVariableLength (120);		// Ans .. 120
			
			codMenusRoot = customField_57.GetStringFieldWithVariableLength (20);		// Ans .. 20
			
			menuItemCount = (int) customField_57.GetNumericFieldAsLong (4);		// N 4
			
			userMenuItems = new List<UserMenuItem> (menuItemCount);
			
			#endregion
			
			
			#region Menu
			
			UserMenuItem item;
			
			
			if (pRequests.Count > 1)
			{
				pRequests.RemoveAt (0);
				
				foreach (Message req in pRequests)
				{
					customField_57 = req.ApplicationData.DataElements.GetCustomPrivateField_57 ();
				
					// Session ID
					customField_57.GetStringFieldWithVariableLength (40);
					// Indice Pagina
					customField_57.GetNumericFieldAsLong (4);
				
					for (int j = 0; j < 12 && menuItemCount > 0; j++, menuItemCount --)
					{
						item = new UserMenuItem ()
						{
							Code = customField_57.GetStringFieldWithVariableLength (60),		// Ans .. 60
							Parent = new UserMenuItem () { Code = customField_57.GetStringFieldWithVariableLength (60) },	// Ans .. 60
							Name = customField_57.GetStringFieldWithVariableLength (60),		// Ans .. 60
							Description = customField_57.GetStringFieldWithVariableLength (60),		// Ans .. 60
							Extra_I = customField_57.GetStringFieldWithVariableLength (60),		// Ans .. 60
							Extra_II = customField_57.GetStringFieldWithVariableLength (60),		// Ans .. 60
							IsEnabled = true
						};
						
						if (string.IsNullOrEmpty (item.Parent.Code))
						{
							item.Parent = null;
						}
						
						userMenuItems.Add (item);
					}
				}
			}
			
			#endregion
			
			pProcessedObjects = new List<object> ();
			
			pProcessedObjects.Add (user);
			pProcessedObjects.Add (codMenusRoot);
			pProcessedObjects.Add (userMenuItems);
		}
	}
}